#!/bin/bash

# AMyCMS - Asterisk and MySQL Cluster Management Suite
# This script is part of the Asterisk and MySQL Cluster configuration and management suite for Debian Lenny
# Please see the README for more info
# This script installs and configures MySQL Cluster for Debian Lenny
# Run the mgm-install.sh and mysql-cluster-install.sh file on your management node and cluster nodes BEFORE running this on your client nodes!!
#
# Copyright Phil Haddad 2009


# wget asterisk and asterisk addons

wget http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-1.6.0.9.tar.gz
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-addons-1.6.0.3.tar.gz

# compile asterisk

tar xfvz asterisk-1.6.0.9.tar.gz
tar xfvz asterisk-addons-1.6.0.3.tar.gz
cd /root/asterisk-1.6.0.9
echo "Compiling Asterisk!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
./configure && make && make install
make config
make samples

# compile asterisk addons

echo "Compiling Asterisk Addons!!!!!!!!!!!!!!!!!!!!!!!!!"
cd /root/asterisk-addons-1.6.0.3
./configure && make && make install
make samples

# modify files

echo "Modifiying modules.conf!!!!!!!!" 
echo "load => cdr_addon_mysql.so" >> /etc/asterisk/modules.conf

echo "Modifying res_mysql.conf!!!!!!!!"
mv /etc/asterisk/res_mysql.conf /etc/asterisk/res_mysql.conf.orig
echo "[general] 
      dbhost = 127.0.0.1 
      dbname = asterisk 
      dbuser = root
      dbpass = password
      dbport = 3306
      dbsock = /var/run/mysqld/mysql.sock" >> /etc/asterisk/res_mysql.conf

echo "Modifying cdr_mysql.conf!!!!!!!!!!!!!!"
mv /etc/asterisk/cdr_mysql.conf /etc/asterisk/cdr_mysql.conf.orig
echo "[global]
      hostname = 127.0.0.1
      dbname = asterisk
      table = cdr
      password = password
      user = root
      port = 3306
      sock = /var/run/mysqld/mysql.sock

      [aliases]
      start = calldate
      callerid = clid" >> /etc/asterisk/cdr_mysql.conf

# set database permissions

echo "Creating password for root user!!!!!!!!!!!!!!!!"
mysqladmin -u root password 'password'

# create asterisk database

echo "Creating asterisk database and CDR table!!!!!!!!!!!!!!!!!!!"
#echo "Please enter create database asterisk; at the prompt!!!!!!!"
query="create database asterisk"
mysql --user=root --password=password << eof
$query
eof

# create asterisk database schema

echo "GRANT INSERT
      ON asterisk.*
      TO root@localhost
      IDENTIFIED BY 'password';

      USE asterisk;

      CREATE TABLE `cdr` (
      `calldate` datetime NOT NULL default '0000-00-00 00:00:00',
      `clid` varchar(80) NOT NULL default '',
      `src` varchar(80) NOT NULL default '',
      `dst` varchar(80) NOT NULL default '',
      `dcontext` varchar(80) NOT NULL default '', 
      `channel` varchar(80) NOT NULL default '',
      `dstchannel` varchar(80) NOT NULL default '',
      `lastapp` varchar(80) NOT NULL default '',
      `lastdata` varchar(80) NOT NULL default '',
      `duration` int(11) NOT NULL default '0',
      `billsec` int(11) NOT NULL default '0',
      `disposition` varchar(45) NOT NULL default '', 
      `amaflags` int(11) NOT NULL default '0',
      `accountcode` varchar(20) NOT NULL default '',
      `userfield` varchar(255) NOT NULL default ''
      );

      ALTER TABLE `cdr` ADD `uniqueid` VARCHAR(32) NOT NULL default '';
      ALTER TABLE `cdr` ADD INDEX ( `calldate` );
      ALTER TABLE `cdr` ADD INDEX ( `dst` );
      ALTER TABLE `cdr` ADD INDEX ( `accountcode` );" >> /root/cdr_schema.txt


mysql --user=root --password=password asterisk < /root/cdr_schema.txt

# load and test asterisk

php loadtest.php

#asterisk -cvvvv | grep -i mysql
